fix: allow Final[T] instance fields in dataclasses to depend on type variables#21643
Open
adhavan18 wants to merge 1 commit into
Open
fix: allow Final[T] instance fields in dataclasses to depend on type variables#21643adhavan18 wants to merge 1 commit into
adhavan18 wants to merge 1 commit into
Conversation
…variables PEP 591 and the dataclass spec say that a Final field declared in a dataclass body is an *instance attribute*, not a class attribute, unless explicitly annotated with ClassVar. The checker was unconditionally raising 'Final name declared in class body cannot depend on type variables' for any Final[T] in a generic class body, which is correct for plain classes but wrong for dataclasses. fix: when the active class has 'dataclass_tag' in its metadata and the lvalue is a Var that is not marked is_classvar, skip the error. non-dataclass classes and explicit ClassVar fields are unaffected. fixes python#21637
Contributor
|
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #21637.
Final[T]fields in a@dataclassbody are instance attributes, not class attributes (PEP 591 / dataclass spec: "AFinaldataclass field initialized in the class body is not a class attribute unless explicitly annotated withClassVar."). The checker was unconditionally raisingFinal name declared in class body cannot depend on type variablesfor anyFinal[T]in a generic class body.Before:
After: no error (same behaviour as
value: TwithoutFinal).Change
In
mypy/checker.py, the condition that raisesDEPENDENT_FINAL_IN_CLASS_BODYnow skips the error when:dataclass_tagin its metadata (i.e. it is a dataclass or dataclass-transform class), andVarthat is not markedis_classvar.Plain (non-dataclass) classes and explicit
ClassVarfields are unaffected.Tests
Two new test cases in
test-data/unit/check-final.test:testFinalDefiningTypevarsDataclassInstanceField—Final[T]instance field in a frozen generic dataclass must not produce an error.testFinalDefiningTypevarsNonDataclassStillErrors—Final[T]in a plain generic class must still produce the error.All 83 existing check-final tests pass with no regressions.